#!/usr/bin/env python
import os
import sys
import time
import wsgi_isapi_ext

# Was making all messages to stdout get redirected to win32 trace
#import win32traceutil
print '# Loading', __file__, 'at ', time.ctime()


basedir = os.path.dirname(os.path.abspath(__file__))
tackdir = os.path.dirname(basedir)


## this should be a "wtf?" moment for you; it still is for me.
sys.exit = os._exit = None


if not os.environ.get('TACKMODDIRS'):
    os.environ['TACKMODDIRS'] = os.path.join(tackdir, 'tack-mods')
if not os.environ.get('TACKSITEDIR'):
    os.environ['TACKSITEDIR'] = basedir


try:
    import tack.lib.apps
except (ImportError, ):
    ## unlike the mod_wsgi adapter, the tack package should be
    ## one directory up from this one, i.e., we assume a parallel
    ## installation of tack and this site.
    if tackdir not in sys.path:
        sys.path.insert(0, tackdir)
    import tack.lib.apps



def application(env, respond):
    env['PATH_INFO'] = p = '/' + '/'.join( env['PATH_INFO'].split('/')[2:])
    env['SCRIPT_NAME'] = env['SCRIPT_NAME'][0:-len(p)]
    return tack.lib.apps.main(env, respond)


handler = wsgi_isapi_ext.WsgiThreadPoolHandler(application)


def __ExtensionFactory__():
    """ The entry point for our ISAPI extension.

    """
    return handler


if __name__=='__main__':
    wsgi_isapi_ext.install()
